/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toolz;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Bunkowski
 */
public class DropTableHeader extends JLabel implements DropTargetListener{

	public static final long serialVersionUID = 1L;
	
    public JTable table;
    public DropTarget dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,this, true, null);

    public DropTableHeader() {
       setText("Header");
    }


    public void dragEnter(DropTargetDragEvent dtde) {
    setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    }
    public void dragOver(DropTargetDragEvent dtde) { }
    public void dropActionChanged(DropTargetDragEvent dtde) {    }
    public void dragExit(DropTargetEvent dte) {
    setBorder(null);
    }

    public void drop(DropTargetDropEvent evt) {

        try {
            Transferable t = evt.getTransferable();
            String unknownStringInput = (String) t.getTransferData(DataFlavor.stringFlavor);
            addInformationFomDrop(unknownStringInput);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        setBorder(null);
    }

    private void addInformationFomDrop(String string) {
        if ( ! string.contains("\n")){
            DefaultTableModel dtm = (DefaultTableModel)table.getModel();
            dtm.setColumnIdentifiers(string.split("\t"));

        }
    }



}
